package ModelLayer;
import java.util.ArrayList;
import java.lang.String;
import java.util.Iterator;

/**
 * Write a description of class PersonContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonContainer
{
    private ArrayList<Person>listOfBorrowers;
    private static PersonContainer instance;
    /**
     * Constructor for objects of class PersonContainer
     */
    private PersonContainer()
    {
        listOfBorrowers = new ArrayList<Person>();

    }

    public static PersonContainer getInstance()
    {
        if(instance == null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }

    private boolean search(String name)
    {    
        for (Person friend : listOfBorrowers)
        {
            if (friend.getName().equals(name)) 
            {
                return true;
            }
        }
        return false; 
    }

    private void addPerson(Person person)
    {
        listOfBorrowers.add(person);
    }

    public Person findPerson(String name)
    {
        int index = 0;
        boolean found = false;
        while(index < listOfBorrowers.size() && !found)
        {
            Person person = listOfBorrowers.get(index);
            if(listOfBorrowers.get(index).getName().equals(name))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found==true)
        {
            return listOfBorrowers.get(index);
        }
        else
        {
            return null;
        }
    }

    public void deletePerson(String name)
    {
        Iterator<Person> it = listOfBorrowers.iterator();
        while(it.hasNext())
        {
            Person t = it.next();
            String nameToRemove = t.getName();
            if(nameToRemove.equals(name))
            {
                it.remove();
            }
        }
    }

    public void createPerson(String name, String address, String city, String zip, String phone)
    {
        if(!search(name))
        {
            Person p = new Person(name, address, city, zip, phone);
            listOfBorrowers.add(p);
        }
    }

    public ArrayList<Person> getList()
    {
        return listOfBorrowers;
    }
}